package operatebag;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import mysqloperate.*;

public class detestudent extends JFrame {
    JFrame jf;
    JLabel label;
    JTextField txt;
    JButton button;
    JButton cancal;


    public detestudent(){
        setTitle("删除学生");
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        setLayout(new FlowLayout());
        setBounds(500,200,400,400);
        setResizable(false);
        setVisible(true);
        jf=this;

        label=new JLabel("输入删除学生学号");
        txt=new JTextField(15);
        button=new JButton("确定");
        cancal=new JButton("取消");
        add(label);
        add(txt);
        add(button);
        add(cancal);
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int n = JOptionPane.showConfirmDialog(null, "确认删除吗?", "确认对话框", JOptionPane.YES_NO_OPTION);
                String str=txt.getText();
                if (n == JOptionPane.YES_OPTION) {
                    boolean idhave;
                    try {
                        idhave=searchstu.youwuid(str);
                    } catch (Exception ex) {
                        throw new RuntimeException(ex);
                    }
                    if(idhave==false){
                        JOptionPane.showMessageDialog(jf, "没有此学生","",JOptionPane.INFORMATION_MESSAGE);
                    }else if(idhave==true){
                        detestudentmysql de=new detestudentmysql();
                        try {
                            de.detestudent(str);
                            JOptionPane.showMessageDialog(jf, "删除成功","",JOptionPane.INFORMATION_MESSAGE);
                        } catch (Exception ex) {
                            JOptionPane.showMessageDialog(jf, "删除失败","",JOptionPane.WARNING_MESSAGE);
                            throw new RuntimeException(ex);
                        }
                    }

                } else if (n == JOptionPane.NO_OPTION) {
                    JOptionPane.showMessageDialog(new JFrame(),"已取消");
                }
            }
        });
        cancal.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                jf.dispose();
            }
        });
    }
}
